package top.z.work.module.system.mapper;

import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryWrapper;
import top.z.work.module.system.entity.SystemRole;
import top.z.work.module.system.entity.SystemUserRole;
import top.z.work.module.system.entity.table.SystemRoleTableDef;
import top.z.work.module.system.entity.table.SystemUserRoleTableDef;

import java.util.List;

/**
 * 用户角色Mapper
 *
 * @author zaoangod
 * @since 2024-03-22
 */
public interface SystemUserRoleMapper extends BaseMapper<SystemUserRole> {

    /**
     * 查询用户拥有的角色列表
     *
     * @param userId 用户ID
     * @return 角色列表
     */
    default List<SystemRole> selectUserRoleList(Long userId) {
        QueryWrapper query = new QueryWrapper();
        query.select().from(SystemUserRoleTableDef.SYSTEM_USER_ROLE)
                .leftJoin(SystemRoleTableDef.SYSTEM_ROLE)
                .on(SystemUserRoleTableDef.SYSTEM_USER_ROLE.ROLE_ID.eq(SystemRoleTableDef.SYSTEM_ROLE.IDENTITY))
                .where(SystemUserRoleTableDef.SYSTEM_USER_ROLE.USER_ID.eq(userId));
        return this.selectListByQueryAs(query, SystemRole.class);
    }

}
